package com.xkp.news.servlet;

import com.xkp.news.entity.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ShowMainHTMLServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");


           //这个页面是主页  如果用户没有登录  ，是不能进行访问的

          // 如何判断用户是否登录   -查看session对象中是否保存  登录用户对象
       User user= (User) req.getSession().getAttribute("user");

       if(user==null){  //没有登录
           //如果用户没有登录  ，则跳转到登陆页面

           resp.sendRedirect("index.html?a=3");
           return;
       }



        resp.getWriter().write("<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>Title</title>\n" +
                "</head>\n" +
                "<body>\n" +
                "\n" +
                "<h1>这是我的主页</h1>\n" +user.getName()+
                "</body>\n" +
                "</html>");


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
